package com.mud.dnd.frontend.rs;

import java.util.Random;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import com.mud.dnd.domain.common.DAOException;
import com.mud.dnd.ejb.CardManager;
import com.mud.dnd.ejb.CommentManager;
import com.mud.dnd.ejb.DeckManager;
import com.mud.dnd.ejb.UserManager;
import com.mud.dnd.ejb.common.AuthenticationException;

@Stateless
@Path("/Admin")
public class AdminJSONService {
	
	@EJB
	CardManager cardManager;
	
	@EJB
	DeckManager deckManager;
	
	@EJB
	CommentManager commentManager;
	
	@EJB
	UserManager userManager;

	
	@GET
	@Path("/loadAdmins")
	@Produces(MediaType.APPLICATION_JSON)
	public String loadAdmins()
	{
		try {
			
			userManager.createUser("alma", "alma");
			String almaSession = userManager.loginUser("alma", "alma");
			
			userManager.createAdminUser("admin", "admin", "alma", almaSession);
		} catch (DAOException e) {
			return "ERROR: "+e.getMessage(); 
		} catch (AuthenticationException e) {
			return "ERROR: "+e.getMessage(); 
		}
	
		return "OK";
	}
	
	
	@GET
	@Path("/loadTestData")
	@Produces(MediaType.APPLICATION_JSON)
	public String loadTestData(@QueryParam("size") String size)
	{
		int iteration = 10;
		if(size!=null)
		{
			iteration = Integer.valueOf(size);
		}
		
		try {
			userManager.createUser("alma", "alma");
			userManager.createUser("korte", "korte");
			userManager.createUser("kornal", "kornal");
			
			
			
			Random generator = new Random();
			
			
			String almaSession = userManager.loginUser("alma", "alma");
			String korteSession = userManager.loginUser("korte", "korte");
			
			userManager.createAdminUser("admin", "admin", "alma", almaSession);
			
			int korteAlmaDeck = deckManager.createDeck("KorteAlma", "Korte lopta Almatol", "http://th00.deviantart.net/fs33/200H/f/2008/240/b/5/Dragon_of_spirits_by_Ruth_Tay.jpg", "korte", korteSession);
			
			
			int almaDeckCount = generator.nextInt(iteration);
			for(int i =0; i<almaDeckCount; i++)
			{
				int deckID = deckManager.createDeck("AlmaDeck"+i, "Ezt olvasssad Kornál", "http://th00.deviantart.net/fs33/200H/f/2008/240/b/5/Dragon_of_spirits_by_Ruth_Tay.jpg", "alma", almaSession);
				deckManager.updateDeck(deckID, null, null, null, (generator.nextInt(4)+1), "alma", almaSession);
				
				int almaDeckAlmaCommentCount = generator.nextInt(iteration);
				for(int j=0; j<almaDeckAlmaCommentCount; j++)
				{
					commentManager.addComment("Alma comments Deck"+String.valueOf(i)+String.valueOf(j), "alma", deckID, almaSession);
					
				}
				int almaDeckKorteCommentCount = generator.nextInt(iteration);
				for(int j=0; j<almaDeckKorteCommentCount; j++)
				{
					commentManager.addComment("Korte comments Deck"+String.valueOf(i)+String.valueOf(j), "korte", deckID, korteSession);
					
				}
				int almaDeckCardCount = generator.nextInt(iteration);
				for(int j =0; j<almaDeckCardCount; j++)
				{
					int cardID = cardManager.createCard("AlmaCard"+String.valueOf(i)+String.valueOf(j), "Ezt meg pláne", "http://th00.deviantart.net/fs33/200H/f/2008/240/b/5/Dragon_of_spirits_by_Ruth_Tay.jpg", "action", 2, "alma", almaSession);
					cardManager.addCardtoDeck(cardID, deckID, "alma", almaSession);
					cardManager.updateCard(cardID, null, null, null, null, (generator.nextInt(4)+1), 0, "alma", almaSession);
					if(generator.nextInt(100)<70)
					{
						cardManager.addCardtoDeck(cardID, korteAlmaDeck, "korte", korteSession);
					}
					
					int almaKorteDeckAlmaCommentCount = generator.nextInt(iteration);
					for(int k=0; k<almaKorteDeckAlmaCommentCount; k++)
					{
						commentManager.addComment("Alma comments Card"+String.valueOf(i)+String.valueOf(j)+String.valueOf(k), "alma", cardID, almaSession);
						
					}
					
					int almaKorteDeckKorteCommentCount = generator.nextInt(iteration);
					for(int k=0; k<almaKorteDeckKorteCommentCount; k++)
					{
						commentManager.addComment("Korte comments Card"+String.valueOf(i)+String.valueOf(j)+String.valueOf(k), "korte", cardID, korteSession);
						
					}
					
				}
				
				
			}
			
			int korteDeckCount = generator.nextInt(iteration);
			for(int i =0; i<korteDeckCount; i++)
			{
				int deckID = deckManager.createDeck("KorteDeck"+i, "Ezt ne nézzed Kornál", "http://th00.deviantart.net/fs33/200H/f/2008/240/b/5/Dragon_of_spirits_by_Ruth_Tay.jpg", "korte", korteSession);
				deckManager.updateDeck(deckID, null, null, null, (generator.nextInt(4)+1), "korte", korteSession);
				
				int korteDeckKorteCommentCount = generator.nextInt(iteration);
				for(int j=0; j<korteDeckKorteCommentCount; j++)
				{
					commentManager.addComment("Korte comments Deck"+String.valueOf(i)+String.valueOf(j), "korte", deckID, korteSession);
					
				}
				int korteDeckAlmaCommentCount = generator.nextInt(iteration);
				for(int j=0; j<korteDeckAlmaCommentCount; j++)
				{
					commentManager.addComment("Alma comments Deck"+String.valueOf(i)+String.valueOf(j), "alma", deckID, almaSession);
					
				}
				int korteDeckAlmaCardCount = generator.nextInt(iteration);
				for(int j =0; j<korteDeckAlmaCardCount; j++)
				{
					int cardID = cardManager.createCard("KorteCard"+String.valueOf(i)+String.valueOf(j), "Ezt meg pláne ne nézzed", "http://th00.deviantart.net/fs33/200H/f/2008/240/b/5/Dragon_of_spirits_by_Ruth_Tay.jpg", "action", 2, "korte", korteSession);
					cardManager.addCardtoDeck(cardID, deckID,"korte", korteSession);
					cardManager.updateCard(cardID, null, null, null, null, (generator.nextInt(4)+1), 0, "korte", korteSession);
					int korteDeckKorteCardCommentCount = generator.nextInt(iteration);
					for(int k=0; k<korteDeckKorteCardCommentCount; k++)
					{
						commentManager.addComment("Korte comments Card"+String.valueOf(i)+String.valueOf(j)+String.valueOf(k), "korte", cardID, korteSession);
					}
					int korteDeckAlmaCardCommentCount = generator.nextInt(iteration);
					for(int k=0; k<korteDeckAlmaCardCommentCount; k++)
					{
						commentManager.addComment("Alma comments Card"+String.valueOf(i)+String.valueOf(j)+String.valueOf(k), "alma", cardID, almaSession);
						
					}
				}
				
				
			}
			
			
		} catch (DAOException e) {
			return "ERROR: "+e.getMessage(); 
		} catch (AuthenticationException e) {
			return "ERROR: "+e.getMessage(); 
		}
		
		
		
		return "OK";
	}
	

}
